package com.example.xunua_zhinengjiaotong;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONObject;

public class honglvdengActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView img_back;
    private MyApp myApp;
    private HttpUtil util=new HttpUtil();
    private Spinner spinner1,spinner2;
    private Button btn1,btn2;
    private TextView tv_btn01,tv_btn02,tv_btn03,tv_btn04,tv_status,tv_time,tv_id01,tv_id02,tv_id03,tv_id04
            ,tv_red,tv_green,tv_yellow;
    private LinearLayout line01,line02,line03,line04,layout01,layout02;
    private Spinner spinner01,spinner02,spinner03,spinner04,spinner05;
    private String[] colors ={"红","绿","黄"},timers={"2秒","3秒","4秒","5秒","10秒","15秒","30秒"};
    private String[] Jsoncolors={"Red","Green","Yellow"},Jsontimers={"2","3","4","5","10","15","30"};


    /**
     *  查询红绿灯当前状态 查询 修改功能 能和接口通信成功 ，但是接口传来的东西，和我修改的东西，不一样。。
     *
     *  查询红绿灯配置信息功能正常
     *
     */

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    String[] s= (String[]) msg.obj;
                    spinner01.setSelection(getSpinnerSelectId(s[0]));
                    spinner02.setSelection(getSpinnerSelectId(s[1]));
                    for (int i = 0; i < 3; i++) {
                        if (s[0].equals(Jsoncolors[i])){
                            tv_status.setText(colors[i]);
                            break;
                        }
                    }
                    tv_time.setText(s[1]+"秒");
                    break;
                case 1:
                    Toast.makeText(myApp, "设置成功", Toast.LENGTH_SHORT).show();
                    break;
                case 10:
                    String[] strs= (String[]) msg.obj;
                    spinner03.setSelection(getSpinnerSelectId(strs[0]));
                    spinner04.setSelection(getSpinnerSelectId(strs[1]));
                    spinner05.setSelection(getSpinnerSelectId(strs[2]));
                    tv_yellow.setText(strs[0]+"秒");
                    tv_green.setText(strs[1]+"秒");
                    tv_red.setText(strs[2]+"秒");
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_honglvdeng);
        initView();
    }

    /**
     * 3.2.1、查询红绿灯当前状态
     */
    private void getData01() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    JSONObject getTrafficLightNowStatus = util.connectServer("{\"TrafficLightId\":\"1-"+(spinner1.getSelectedItemId()+1)+"\"}", myApp, "GetTrafficLightNowStatus");
                    Log.e("TAG", "run: "+getTrafficLightNowStatus.toString() );
                    String status = getTrafficLightNowStatus.getString("Status");
                    String time = getTrafficLightNowStatus.getString("Time");
                    String s[]={status,time};
                    Message message = new Message();
                    message.what=0;
                    message.obj=s;
                    handler.sendMessage(message);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private int getSpinnerSelectId(String s){
        int id=-1;
        switch (s){
            case "2":
            case "Red":
                id=0;
                break;
            case "3":
            case "Green":
                id=1;
                break;
            case "4":
            case "Yellow":
                id=2;
                break;
            case "5":
                id=3;
                break;
            case "10":
                id=4;
                break;
            case "15":
                id=5;
                break;
            case "30":
                id=6;
                break;
        }
        return id;
    }

    private void initView() {
        myApp= (MyApp) getApplication();
        layout01=findViewById(R.id.hld_layout01);
        layout02=findViewById(R.id.hld_layout02);
        tv_red=findViewById(R.id.hld_tv_red);
        tv_green=findViewById(R.id.hld_tv_green);
        tv_yellow=findViewById(R.id.hld_tv_yellow);
        tv_id01=findViewById(R.id.hld_tv_id01);
        tv_id02=findViewById(R.id.hld_tv_id02);
        tv_id03=findViewById(R.id.hld_tv_id03);
        tv_id04=findViewById(R.id.hld_tv_id04);
        tv_btn01=findViewById(R.id.hld_tv_btn01);
        tv_btn01.setOnClickListener(this);
        tv_btn02=findViewById(R.id.hld_tv_btn02);
        tv_btn02.setOnClickListener(this);
        tv_btn03=findViewById(R.id.hld_tv_btn03);
        tv_btn03.setOnClickListener(this);
        tv_btn04=findViewById(R.id.hld_tv_btn04);
        tv_btn04.setOnClickListener(this);
        line01=findViewById(R.id.honglvdeng_line_01);
        line02=findViewById(R.id.honglvdeng_line_02);
        line03=findViewById(R.id.honglvdeng_line_03);
        line04=findViewById(R.id.honglvdeng_line_04);
        img_back=findViewById(R.id.honglvdeng_img_back);
        img_back.setOnClickListener(this);
        spinner1=findViewById(R.id.honglvdeng_spinner1);
        spinner1.setAdapter(new ArrayAdapter<>(honglvdengActivity.this,R.layout.support_simple_spinner_dropdown_item,
                new String[]{"1号红绿灯","2号红绿灯","3号红绿灯","4号红绿灯","5号红绿灯"}));
        spinner2=findViewById(R.id.honglvdeng_spinner2);
        spinner2.setAdapter(new ArrayAdapter<>(honglvdengActivity.this,R.layout.support_simple_spinner_dropdown_item,
                new String[]{"1号红绿灯","2号红绿灯","3号红绿灯","4号红绿灯","5号红绿灯"}));
        btn1=findViewById(R.id.honglvdeng_btn_chaxun1);
        btn1.setOnClickListener(this);
        btn2=findViewById(R.id.honglvdeng_btn_chaxun2);
        btn2.setOnClickListener(this);
        spinner01=findViewById(R.id.hld_spinner01);
        spinner02=findViewById(R.id.hld_spinner02);
        spinner03=findViewById(R.id.hld_spinner03);
        spinner04=findViewById(R.id.hld_spinner04);
        spinner05=findViewById(R.id.hld_spinner05);
        spinner01.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, colors));
        spinner02.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, timers));
        spinner03.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, timers));
        spinner04.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, timers));
        spinner05.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, timers));
        tv_status=findViewById(R.id.hlv_tv_status);
        tv_time=findViewById(R.id.hlv_tv_time);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                int i=position;
                ++i;
                tv_id01.setText(i+"");
                tv_id02.setText(i+"");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                int i=position;
                ++i;
                tv_id03.setText(i+"");
                tv_id04.setText(i+"");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.honglvdeng_img_back:
                finish();
                break;
            case R.id.honglvdeng_btn_chaxun1:
                layout01.setVisibility(View.VISIBLE);
                line01.setVisibility(View.VISIBLE);
                line02.setVisibility(View.GONE);
                getData01();
                break;
            case R.id.hld_tv_btn01://修改
                line01.setVisibility(View.GONE);
                line02.setVisibility(View.VISIBLE);
                break;
            case R.id.hld_tv_btn02://保存
                line01.setVisibility(View.VISIBLE);
                line02.setVisibility(View.GONE);
                tv_status.setText(spinner01.getSelectedItem()+"");
                tv_time.setText(spinner02.getSelectedItem()+"");
                setData01();
                break;
            case R.id.honglvdeng_btn_chaxun2:
                layout02.setVisibility(View.VISIBLE);
                line03.setVisibility(View.VISIBLE);
                line04.setVisibility(View.GONE);
                getData02();
                break;
            case R.id.hld_tv_btn03://修改
                line03.setVisibility(View.GONE);
                line04.setVisibility(View.VISIBLE);
                break;
            case R.id.hld_tv_btn04://保存
                line03.setVisibility(View.VISIBLE);
                line04.setVisibility(View.GONE);
                tv_yellow.setText(spinner03.getSelectedItem()+"");
                tv_green.setText(spinner04.getSelectedItem()+"");
                tv_red.setText(spinner05.getSelectedItem()+"");
                setData02();
                break;
        }
    }

    private void setData02() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    JSONObject jsonObject = util.connectServer("{\"TrafficLightId\":"+(spinner2.getSelectedItemId()+1)+",\"RedTime\":"+Jsontimers[(int) spinner05.getSelectedItemId()]+",\"GreenTime\":"+Jsontimers[(int) spinner03.getSelectedItemId()]+",\"YellowTime\":"+Jsontimers[(int) spinner04.getSelectedItemId()]+"}", myApp, "SetTrafficLightConfig");
                    String result = jsonObject.getString("result");
                    if ("ok".equals(result)){
                        handler.sendEmptyMessage(1);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void getData02() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    JSONObject jsonObject = util.connectServer("{\"TrafficLightId\":" + (spinner2.getSelectedItemId() + 1 )+ "}", myApp, "GetTrafficLightConfigAction");
                    String yellowTime = jsonObject.getString("YellowTime");
                    String greenTime = jsonObject.getString("GreenTime");
                    String redTime = jsonObject.getString("RedTime");
                    String[] s={yellowTime,greenTime,redTime};
                    Message message = new Message();
                    message.what=10;
                    message.obj=s;
                    handler.sendMessage(message);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void setData01() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    JSONObject jsonObject = util.connectServer("{\"TrafficLightId\":\"1-" + spinner1.getSelectedItemId() + 1 + "\",\"Status\":\"" + Jsoncolors[(int) spinner01.getSelectedItemId()] + "\",\"Time\":" + Jsontimers[(int) spinner02.getSelectedItemId()] + "}", myApp, "SetTrafficLightNowStatus");
                    String result = jsonObject.getString("result");
                    if ("ok".equals(result)){
                        handler.sendEmptyMessage(1);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onBackPressed() {
        // super.onBackPressed();
    }
}
